<template>
  <div class="progress" :class="[`progress-${type}`, { 'is-text-inside': textInside }]">
    <div class="progress-bar" :style="{ height: strokeWidth + 'px' }">
      <div class="progress-bar-inner" :style="barStyle">
        <div v-if="textInside" class="progress-bar-text">{{ percentage }}%</div>
      </div>
    </div>
    <div v-if="!textInside" class="progress-text">{{ percentage }}%</div>
  </div>
</template>

<script>
export default {
  name: 'FeedbackProgress',
  props: {
    percentage: {
      type: Number,
      required: true,
      validator: value => value >= 0 && value <= 100
    },
    type: {
      type: String,
      default: 'line',
      validator: value => ['line', 'circle'].includes(value)
    },
    strokeWidth: {
      type: Number,
      default: 6
    },
    textInside: {
      type: Boolean,
      default: false
    },
    status: {
      type: String,
      default: '',
      validator: value => ['', 'success', 'exception', 'warning'].includes(value)
    },
    color: {
      type: [String, Array, Function],
      default: ''
    }
  },
  computed: {
    barStyle() {
      const style = {
        width: this.percentage + '%'
      }

      if (this.status) {
        style.backgroundColor = this.getStatusColor()
      } else if (this.color) {
        style.backgroundColor = this.getCurrentColor(this.percentage)
      }

      return style
    }
  },
  methods: {
    getStatusColor() {
      const statusMap = {
        success: '#67c23a',
        exception: '#f56c6c',
        warning: '#e6a23c'
      }
      return statusMap[this.status]
    },
    getCurrentColor(percentage) {
      if (typeof this.color === 'function') {
        return this.color(percentage)
      } else if (typeof this.color === 'string') {
        return this.color
      } else if (Array.isArray(this.color)) {
        return this.getLevelColor(percentage)
      }
    },
    getLevelColor(percentage) {
      const colorArray = this.color.sort((a, b) => a.percentage - b.percentage)
      for (let i = 0; i < colorArray.length; i++) {
        if (colorArray[i].percentage > percentage) {
          return colorArray[i].color
        }
      }
      return colorArray[colorArray.length - 1].color
    }
  }
}
</script>

<style scoped>
.progress {
  position: relative;
  line-height: 1;
}

.progress-bar {
  width: 100%;
  background-color: #ebeef5;
  border-radius: 100px;
  overflow: hidden;
}

.progress-bar-inner {
  position: relative;
  background-color: #409eff;
  transition: width 0.6s ease;
  border-radius: 100px;
  height: 100%;
}

.progress-text {
  font-size: 14px;
  color: #606266;
  margin-left: 8px;
  display: inline-block;
  vertical-align: middle;
}

.progress.is-text-inside .progress-bar-inner {
  display: flex;
  align-items: center;
  justify-content: center;
}

.progress.is-text-inside .progress-bar-text {
  color: #fff;
  font-size: 12px;
  margin: 0;
}

.progress-success .progress-bar-inner {
  background-color: #67c23a;
}

.progress-exception .progress-bar-inner {
  background-color: #f56c6c;
}

.progress-warning .progress-bar-inner {
  background-color: #e6a23c;
}
</style> 